#!/bin/bash

# Online Bookstore Flutter App - Run Script
echo "🚀 Starting Online Bookstore Flutter App..."

# Check if Flutter is installed
if ! command -v flutter &> /dev/null; then
    echo "❌ Flutter is not installed. Please install Flutter first."
    echo "Visit: https://flutter.dev/docs/get-started/install"
    exit 1
fi

# Check Flutter version
echo "📱 Flutter version:"
flutter --version

# Get dependencies
echo "📦 Getting dependencies..."
flutter pub get

# Run code generation
echo "🔧 Running code generation..."
flutter packages pub run build_runner build --delete-conflicting-outputs

# Check for connected devices
echo "📱 Checking for connected devices..."
flutter devices

# Run the app
echo "🎯 Starting the app..."
echo ""
echo "📋 Test Accounts:"
echo "   Email: test@example.com"
echo "   Password: password"
echo ""
echo "   Email: admin@example.com"
echo "   Password: admin123"
echo ""

# Try to run on available device
if flutter devices | grep -q "Chrome"; then
    echo "🌐 Running on Chrome..."
    flutter run -d chrome
elif flutter devices | grep -q "macos"; then
    echo "💻 Running on macOS..."
    flutter run -d macos
elif flutter devices | grep -q "ios"; then
    echo "📱 Running on iOS Simulator..."
    flutter run -d ios
elif flutter devices | grep -q "android"; then
    echo "🤖 Running on Android..."
    flutter run -d android
else
    echo "❌ No devices found. Please connect a device or start an emulator."
    echo "Available options:"
    echo "  - Start Android Studio and create an Android Virtual Device"
    echo "  - Start Xcode and open iOS Simulator"
    echo "  - Use Chrome for web development"
    exit 1
fi
